package com.example.mpandroidchartdemo;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

public class BarChartActivity extends AppCompatActivity {

    private BarChart mBarChart;
    List<BarEntry> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bar_chart_activity);
        mBarChart = findViewById(R.id.bar_chart);

        // 添加数据
        list.add(new BarEntry(1,7));
        list.add(new BarEntry(2,10));
        list.add(new BarEntry(3,12));
        list.add(new BarEntry(4,6));
        list.add(new BarEntry(5,3));

        BarDataSet barDataSet = new BarDataSet(list,"语文");
        BarData barData = new BarData(barDataSet);
        mBarChart.setData(barData);

        // 背景
        mBarChart.setBackgroundColor(0x30000000); // 背景颜色
        mBarChart.getXAxis().setDrawGridLines(false);
        mBarChart.getAxisLeft().setDrawGridLines(false);

        mBarChart.getDescription().setEnabled(false); // 隐藏右下角英文

        // 图例
        Legend legend = new Legend();
        legend.setEnabled(true);
        legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);

        // X轴
        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴位置
        xAxis.setAxisLineColor(Color.BLUE);
        xAxis.setAxisLineWidth(2);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (value == 1) {
                    return "第一位";
                }
                if (value == 2){
                    return "第二位";
                }
                if (value == 3) {
                    return "第三位";
                }
                if (value == 4){
                    return "第四位";
                }
                if (value == 5) {
                    return "第五位";
                }
                return "";
            }
        });
        xAxis.setAxisMaximum(6);
        xAxis.setAxisMinimum(0);
        xAxis.setLabelCount(6,false);

        // Y轴
        final YAxis yAxis = mBarChart.getAxisLeft();
        mBarChart.getAxisRight().setEnabled(false); // 隐藏右侧Y轴
        yAxis.setAxisLineColor(Color.GREEN);
        yAxis.setAxisLineWidth(2);
        yAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                for (int i = 0; i < 16; i++) {
                    if (value == i){
                        return i + "℃";
                    }
                }
                return "";
            }
        });
        yAxis.setAxisMaximum(16); // Y轴最大数值
        yAxis.setAxisMinimum(0); // Y轴最小数值
        yAxis.setLabelCount(5,false); // Y轴坐标标签个数

        // 柱子
        barDataSet.setColors(Color.GREEN,Color.YELLOW,Color.RED,Color.BLACK,Color.BLUE); // 柱子颜色
        barDataSet.setBarBorderWidth(2); // 柱子边框粗细
        barDataSet.setBarBorderColor(Color.DKGRAY); // 边框颜色
        barDataSet.setHighlightEnabled(false); // 选中时是否高亮显示
        barDataSet.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                if (entry.getY() == value){
                    return value + "℃";
                }
                return "";
            }
        });
        barData.setBarWidth(0.75F); // 柱子宽度

        // 数据更新
//        mBarChart.notifyDataSetChanged();
//        mBarChart.invalidate();

        // 动画(如果使用了动画可以则省去更新数据步骤)
        mBarChart.animateY(1500);
    }
}
